package jhunter.web.mvc.params;

import java.util.HashMap;

public class FlashMap extends HashMap<String, Object> implements Comparable<FlashMap> {

	private static final long serialVersionUID = 6356285429022626381L;

	private String requestUri;

	private long expirationStartTime;

	private int timeToLive;

	public String getRequestUri() {
		return requestUri;
	}

	public void setRequestUri(String requestUri) {
		this.requestUri = requestUri;
	}

	@Override
	public int compareTo(FlashMap other) {
		int thisUrlPath = (this.requestUri != null) ? 1 : 0;
		int otherUrlPath = (other.requestUri != null) ? 1 : 0;
		if (thisUrlPath != otherUrlPath) {
			return otherUrlPath - thisUrlPath;
		} else {
			return other.size() - this.size();
		}
	}

	public boolean isExpired() {
		if (this.expirationStartTime != 0) {
			return (System.currentTimeMillis() - this.expirationStartTime) > this.timeToLive * 1000;
		} else {
			return false;
		}
	}

	public long getExpirationStartTime() {
		return expirationStartTime;
	}

	public void setExpirationStartTime(long expirationStartTime) {
		this.expirationStartTime = expirationStartTime;
	}

	public int getTimeToLive() {
		return timeToLive;
	}

	public void setTimeToLive(int timeToLive) {
		this.timeToLive = timeToLive;
	}

	public void startExpirationPeriod(int timeToLive) {
		this.expirationStartTime = System.currentTimeMillis();
		this.timeToLive = timeToLive;
	}
}
